import time
from machine import Pin, SoftSPI
import mfrc522

def main():
    rdr = mfrc522.MFRC522(0, 2, 4, 5, 14)
    try:
        count = 0
        while(count < 9):
            count = count + 1
            (stat, tag_type) = rdr.request(rdr.REQIDL)
            if stat == rdr.OK:
                (stat, raw_uid) = rdr.anticoll()
                if stat == rdr.OK:
                    print("New card detected")
                    print("  - tag type: 0x%02x" % tag_type)
                    print("  - uid	 : 0x%02x%02x%02x%02x" % (raw_uid[3], raw_uid[2], raw_uid[1], raw_uid[0]))
                    print("")
                    if rdr.select_tag(raw_uid) == rdr.OK:
                        key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
                        if rdr.auth(rdr.AUTHENT1A, 8, key, raw_uid) == rdr.OK:
                            print("Address 8 data: %s" % rdr.read(8))
                            rdr.stop_crypto1()
                        else:
                            print("Authentication error")
                    else:
                        print("Failed to select tag")
            time.sleep(2)
    except KeyboardInterrupt:
	    print("Bye")

if __name__ == '__main__':
    main()